package com.huige.controller;

import com.huige.pojo.Result;
import com.huige.utils.AliOSSUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.print.attribute.standard.OrientationRequested;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * ClassName: UploadController
 * Package: com.huige.controller
 * Description:
 *
 * @Author 朱志辉
 * @Create 2024/5/5 15:49
 * @Version 1.0
 */
@RestController
@Slf4j
public class UploadController {
    @Autowired
    private AliOSSUtils aliOSSUtils;

    //本地存储文件的方式
/*    @PostMapping("/upload")
    public Result upLoad(String username, Integer age, MultipartFile image) throws IOException {
        log.info("文件上传:{},{},{}",username,age,image);
        //获取原始文件名 -1.jpg  123.0.0.jpg
        String filename = image.getOriginalFilename();

        //构造一个唯一的文件名(不能重复) -- uuid(通用唯一识别码)  5d097109-36a8-476c-8b77-15def9110398
        int index = filename.lastIndexOf(".");
        String substring = filename.substring(index);
        String newFileName = UUID.randomUUID().toString() + substring;
        log.info("新的文件名:{}",newFileName);


        //将接收到的文件存储在服务器的磁盘目录当中:E:\java长征路\javaweb\资料\day11-SpringBootWeb案例\资料\image
        image.transferTo(new File("E:\\java长征路\\javaweb\\资料\\day11-SpringBootWeb案例\\资料\\image\\"+newFileName));

        return Result.success();

    }*/


    /**
     * @param image
     * @return
     */
    @PostMapping("/upload")
    public Result upload(MultipartFile image) throws IOException {
        log.info("文件上传,文件的名字:{}",image.getOriginalFilename());
        //调用阿里云OSS工具类进行文件上传
        String url = aliOSSUtils.upload(image);//调用阿里云OSS工具类,将上传上来的文件存放于阿里云
        log.info("文件上传完成,文件访问的url为:{}", url);

        return Result.success(url);//将图片上传完成的后的url返回,用于浏览器回显展示
    }


}
